package com.oa.ylms.apps.dao.assess.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.dao.assess.IAssessDetailInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.assess.AssessDetailInfoValue;
import com.oa.ylms.apps.entity.assess.AssessInfoValue;

@SuppressWarnings("unchecked")
public class AsssessDetailInfoDaoImpl extends HibernateDaoSupport implements
		IAssessDetailInfoDao {

	private List<AssessDetailInfoValue> list;
	private int numbers;

	public int addAssessDetailInfo(AssessDetailInfoValue assessDetailInfo)
			throws Exception {
		if (assessDetailInfo != null) {
			this.getHibernateTemplate().save(assessDetailInfo);

		}
		return assessDetailInfo.getDetailId();
	}

	public boolean deleteAssessDetailInfo(AssessDetailInfoValue assessDetailInfo)
			throws Exception {
		if (assessDetailInfo != null) {
			try {
				this.getHibernateTemplate().delete(assessDetailInfo);

			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}

	public boolean modifyAssessDetailInfo(AssessDetailInfoValue assessDetailInfo)
			throws Exception {
		if (assessDetailInfo != null) {
			try {
				this.getHibernateTemplate().update(assessDetailInfo);

			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}

	public List<AssessDetailInfoValue> queryAllAssessDetailInfo()
			throws Exception {

		return this.getHibernateTemplate().find(
				"from AssessDetailInfoValue adiv");
	}

	public AssessDetailInfoValue queryAssessDetaiInfoByDetailId(int detailId)
			throws Exception {

		return (AssessDetailInfoValue) this.getHibernateTemplate().get(
				AssessDetailInfoValue.class, detailId);
	}

	public int countTpages(final ArchiveInfoValue archiveInfo,
			final AssessInfoValue assessInfo, final int rows) throws Exception {
		int pageNumbers = 0;
		String hqlStr = "SELECT adiv FROM AssessDetailInfoValue adiv,StaffInfoValue siv,ArchiveInfoValue aiv where adiv.staffInfo.staffId= siv.staffId AND siv.archiveInfo.archiveId = aiv.archiveId AND adiv.assessInfo.assessId = ?";
		if (archiveInfo.getStaffName() != null) {
			hqlStr += " AND aiv.staffName = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = 0;
				query.setInteger(index, assessInfo.getAssessId());
				if (archiveInfo.getStaffName() != null) {
					index++;
					query.setString(index, archiveInfo.getStaffName());
				}
				list = query.list();
				return list;
			}
		});
		this.numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		}
		if (numbers == 0) {
			pageNumbers = 0;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}

	public int getTotalNumber() throws Exception {

		return numbers;
	}

	public List<AssessDetailInfoValue> queryAssessDetailInfoWithPageByAssessId(
			final ArchiveInfoValue archiveInfo,
			final AssessInfoValue assessInfo, final int rows, final int pageNo)
			throws Exception {
		String hqlStr = "SELECT adiv FROM AssessDetailInfoValue adiv,StaffInfoValue siv,ArchiveInfoValue aiv where adiv.staffInfo.staffId= siv.staffId AND siv.archiveInfo.archiveId = aiv.archiveId AND adiv.assessInfo.assessId = ?";
		if (archiveInfo.getStaffName() != null) {
			hqlStr += " AND aiv.staffName = ?";
		}
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = 0;
				query.setInteger(index, assessInfo.getAssessId());
				if (archiveInfo.getStaffName() != null) {
					index++;
					query.setString(index, archiveInfo.getStaffName());
				}
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	public List<AssessDetailInfoValue> getList() {
		return list;
	}

	public void setList(List<AssessDetailInfoValue> list) {
		this.list = list;
	}

	public int getNumbers() {
		return numbers;
	}

	public void setNumbers(int numbers) {
		this.numbers = numbers;
	}

}
